//package com.hotelvip.common.jwt;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.hotelvip.common.pojo.JWTToken;
//import com.hotelvip.common.utils.JSONResult;
//import com.hotelvip.common.utils.JWTUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
//import org.springframework.http.HttpStatus;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.RequestMethod;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * @author Lzh
// * @date 2020/12/8 8:59
// */
//@Slf4j
//public class JWTFilter extends BasicHttpAuthenticationFilter {
//    /**
//     * 判断是否允许通过
//     *
//     */
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        log.info("isAccessAllowed方法");
//        try{
//            return executeLogin(request, response);
//        }catch (Exception e){
//            log.info("错误"+e);
//            responseError(response,"shiro fail");
//            return false;
//        }
//    }
//    /**
//     * 是否进行登录请求
//     *
//     */
//    @Override
//    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
//        log.info("isLoginAttempt方法");
//        String token=((HttpServletRequest)request).getHeader("token");
//        return token != null;
//    }
//
//    /**
//     * 创建shiro token
//     *
//     */
//    @Override
//    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
//        log.info("createToken方法");
//        String jwtToken = ((HttpServletRequest)request).getHeader("token");
//        if(jwtToken!=null) {
//            return new JWTToken(jwtToken);
//        }
//        return null;
//    }
//    /**
//     * isAccessAllowed为false时调用，验证失败
//     */
//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
//        log.info("onAccessDenied");
//        AuthenticationToken token = createToken(request, response);
//
//        String username = JWTUtil.getUsername((String) token.getCredentials());
//        System.out.println(username);
//        if (username == null|| StringUtils.isEmpty(username)){
//            responseError(response, "用户未登录");
//            return false;
//        }
//        String jwttoken= (String) token.getPrincipal();
//        boolean flag = JWTUtil.verify(((HttpServletRequest) request).getHeader("token"), username);
//        System.out.println("token验证:"+flag);
//        if (!flag){
//
//            responseError(response, "Token有误");
//            return false;
//        }
//
//        return true;
////        this.sendChallenge(request,response);
////        responseError(response,"token verify fail");
////        return false;
//    }
//
//
//    /**
//     * shiro验证成功调用
//     */
////    @Override
////    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) {
////        log.info("onLoginSuccess：");
////        String jwttoken= (String) token.getPrincipal();
////        if (jwttoken!=null){
////            try{
////                if(JwtUtil.verify(jwttoken)){
////                    //判断Redis是否存在所对应的RefreshToken
////                    long userId = JwtUtil.getUserId(jwttoken);
////                    Long currentTime=JwtUtil.getCurrentTime(jwttoken);
////                    if (RedisUtil.hasKey(userId)) {
////                        Long currentTimeMillisRedis = (Long) RedisUtil.get(userId);
////                        return currentTimeMillisRedis.equals(currentTime);
////                    }
////                }
////                return false;
////            }catch (Exception e){
////                log.info("token验证："+e.getClass());
////                if (e instanceof TokenExpiredException){
////                    log.info("TokenExpiredException");
////                    return refreshToken(request, response);
////                }
////            }
////        }
////        return true;
////    }
//
//    /**
//     * 拦截器的前置方法，此处进行跨域处理
//     */
//    @Override
//    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
//        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse= (HttpServletResponse) response;
//        httpServletResponse.setHeader("Access-Control-Allow-Origin",httpServletRequest.getHeader("Origin"));
//        httpServletResponse.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,PUT,DELETE");
//        httpServletResponse.setHeader("Access-Control-Allow-Headers",httpServletRequest.getHeader("Access-Control-Resquest-Headers"));
//        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())){
//            httpServletResponse.setStatus(HttpStatus.OK.value());
//        }
//
//        //如果不带token，不去验证shiro
//        if (!isLoginAttempt(request,response)){
//            responseError(httpServletResponse,"no token");
//            return false;
//        }
//        return super.preHandle(request,response);
//    }
//
//
//
//
//    private void responseError(ServletResponse response, String msg){
//        HttpServletResponse httpResponse = (HttpServletResponse) response;
//        httpResponse.setStatus(401);
//        httpResponse.setCharacterEncoding("UTF-8");
//        httpResponse.setContentType("application/json;charset=UTF-8");
//        try {
//            String rj = new ObjectMapper().writeValueAsString(new JSONResult(401, msg, null));
//            httpResponse.getWriter().append(rj);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//}
